package entity

import (
	"errors"
	"fmt"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

// INDI_LSPH_ZJXX 用户
type INDI_LSPH_ZJXX struct {
	ID       int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	CUID     int    `gorm:"column:CUID"`
	BZJXM    string `gorm:"column:BZJXM;"`
	BZJIDNO  string `gorm:"column:BZJIDNO;"`
	CCUID    int    `gorm:"column:CCUID;"`
	BZJJGMC  string `gorm:"column:BZJJGMC"`
	BZJJGDM  string `gorm:"column:BZJJGDM;"`
	TJCUID   int    `gorm:"column:TJCUID;"`
	TJKHXM   string `gorm:"column:TJKHXM;"`
	TJIDNO   string `gorm:"column:TJIDNO;"`
	WHKHJLID string `gorm:"column:WHKHJLID;"`
	WHKHJLXM string `gorm:"column:WHKHJLXM;"`
	BZ       string `gorm:"column:BZ"`
}

// TableName User用户表名
func (INDI_LSPH_ZJXX) TableName() string {
	return "T_INDI_LSPH_ZJXX"
}

// BeforeCreate 插入前的触发器
func (u *INDI_LSPH_ZJXX) BeforeCreate(tx *gorm.DB) (err error) {
	var bzjkh int
	sqlEx := fmt.Sprintf(`select ID  from T_INDI_CLIE where IDNO = ?`)
	row := tx.Raw(sqlEx, u.BZJIDNO).Row()
	row.Scan(&bzjkh)
	if bzjkh == 0 {
		info := fmt.Sprintf("被转介客户证件号码【%s】在系统中不存在，请修改！", u.BZJIDNO)
		return errors.New(info)
	}
	var bzjkhbsy int
	sqlEx2 := fmt.Sprintf(`select ID from V_对私账户_分户情况 where IDNO = ? and EMNO = ?`)
	row2 := tx.Raw(sqlEx2, u.BZJIDNO, u.WHKHJLID).Row()
	row2.Scan(&bzjkhbsy)
	if bzjkhbsy == 0 {
		info := fmt.Sprintf("被转介客户【%s】不属于【%s】客户，请修改！", u.BZJXM, u.WHKHJLXM)
		return errors.New(info)
	}
	var bzjkhcf int
	sqlEx4 := fmt.Sprintf(`select ID from T_INDI_LSPH_ZJXX where BZJIDNO = ? and BZJIDNO <> ''`)
	row4 := tx.Raw(sqlEx4, u.BZJIDNO).Row()
	row4.Scan(&bzjkhcf)
	if bzjkhcf != 0 {
		info := fmt.Sprintf("被转介客户【%s】不能重复添加，请修改！", u.BZJXM)
		return errors.New(info)
	}
	var bzjqykh int
	sqlEx = fmt.Sprintf(`select ID  from T_COMP_CLIE where CUNM = ? OR ENCD = ?`)
	row = tx.Raw(sqlEx, u.BZJJGMC, u.BZJJGDM).Row()
	row.Scan(&bzjqykh)
	if bzjqykh == 0 {
		info := fmt.Sprintf("被转介企业客户【%s】在系统中不存在，请修改！", u.BZJJGMC)
		return errors.New(info)
	}
	var bzjqykhbsy int
	sqlEx5 := fmt.Sprintf(`select ID from V_对公账户_分户情况 where CUNM= ? and EMNO=?`)
	row5 := tx.Raw(sqlEx5, u.BZJJGMC, u.WHKHJLID).Row()
	row5.Scan(&bzjqykhbsy)
	if bzjqykhbsy == 0 {
		info := fmt.Sprintf("被转介企业客户【%s】不属于【%s】客户，请修改！", u.BZJJGMC, u.WHKHJLXM)
		return errors.New(info)
	}
	var bzjqykhcf int
	sqlEx6 := fmt.Sprintf(`select ID from T_INDI_LSPH_ZJXX where BZJJGMC = ? and BZJJGMC <> ''`)
	row6 := tx.Raw(sqlEx6, u.BZJJGMC).Row()
	row6.Scan(&bzjqykhcf)
	if bzjqykhcf != 0 {
		info := fmt.Sprintf("被转介企业客户【%s】不能重复添加，请修改！", u.BZJJGMC)
		return errors.New(info)
	}
	var TJCUID int
	sqlEx3 := fmt.Sprintf(`select ID  from T_INDI_CLIE where IDNO = ?`)
	row3 := tx.Raw(sqlEx3, u.TJIDNO).Row()
	row3.Scan(&TJCUID)
	if TJCUID == 0 {
		info := fmt.Sprintf("推荐客户证件号码【%s】在系统中不存在，请修改！", u.TJIDNO)
		return errors.New(info)
	}
	u.CUID = bzjkh
	u.CCUID = bzjqykh
	u.TJCUID = TJCUID
	return
}

// AfterCreate 插入后的触发器
func (u *INDI_LSPH_ZJXX) AfterCreate(tx *gorm.DB) (err error) {
	// tx.Exec("UPDATE T_SysUser SET EMID=0")
	// base.CacheUser(u.ID, u.Name, *u.OrganizationID)
	// sql := "select ParentOrganID from V_ParentOrgan where OrganID=?"
	// row := tx.Raw(sql, *u.OrganizationID).Row()
	// var putid int
	// row.Scan(&putid)
	// if putid == 61 {
	// 	tx.Exec("INSERT INTO t_UserRole(UserId,RoleId,IsAllowCommision,IsAllowProxy)VALUES(?,?,?,?)", u.ID, 112, 0, 0)
	// }
	return
}

func INDI_LSPH_ZJXXEF() base.EntityFunc {
	return func() interface{} {
		entity := INDI_LSPH_ZJXX{}
		return &entity
	}
}
func INDI_LSPH_ZJXXESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []INDI_LSPH_ZJXX
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("INDI_LSPH_ZJXX", base.NewDefaultDao(INDI_LSPH_ZJXXEF(), INDI_LSPH_ZJXXESF(), nil))
}
